/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.swt.SWT;

import edu.pku.sei.modeler.editpolicies.InteractionLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.SequenceNodeEditPolicy;
import edu.pku.sei.modeler.figures.InteractionFigure;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

/**
 * @generated
 * 
 */
public class InteractionEditpart extends AbstractUMLNodeEditPart {
	public InteractionEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label header = new Label("GeneratedShape");
		InteractionFigure figure = new InteractionFigure();
		figure.add(header);

		ConnectionLayer connLayer = (ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER);

		// connLayer.setConnectionRouter(new HorizontalConnectionRouter());
		connLayer.setConnectionRouter(null);
		connLayer.setAntialias(SWT.ON);

		return figure;
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		if (ModelPackage.DIAGRAM_UML_SEQUENCE.equals(getNode().getDiagram().getType()))
			installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SequenceNodeEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new InteractionLayoutEditPolicy());

	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		refreshVisuals();

		super.handlePropertyChanged(event);
	}

	private Interaction getInteraction() {
		return (Interaction) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		Interaction modelEle = this.getInteraction();
		InteractionFigure figure = (InteractionFigure) getFigure();
		figure.getText().setText(modelEle.getName());
		//		
		// {
		// String text31830 = "";
		// text31830 += modelEle.getName()+"\n";
		//
		// TextFigure tFigure31830 = figure.getText31830();
		// tFigure31830.setText(text31830);
		// }

	}
	
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
		return super.getSourceConnectionAnchor(connection);
	}
	
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return super.getSourceConnectionAnchor(request);
	}

	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
		return super.getTargetConnectionAnchor(connection);
	}
	
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return null;
	}
	// static class HorizontalConnectionRouter implements ConnectionRouter {
	//
	// private static final PrecisionPoint A_POINT = new PrecisionPoint();
	//		
	// public Object getConstraint(Connection connection) {
	// return null;
	// }
	//
	// public void invalidate(Connection connection) {
	//			
	// }
	//
	// public void remove(Connection connection) {
	//			
	// }
	//
	// public void route(Connection connection) {
	// PointList points = connection.getPoints();
	// points.removeAllPoints();
	//			
	//
	// Point ref1, ref2;
	//
	// if (connection.getTargetAnchor() instanceof BorderAnchor) {
	// ref1 = ((BorderAnchor)connection.getTargetAnchor()).getCurrentLocation();
	// }
	// else {
	// ref1 = connection.getTargetAnchor().getReferencePoint();
	// }
	// if (connection.getSourceAnchor() instanceof BorderAnchor) {
	// ref2 = ((BorderAnchor)connection.getSourceAnchor()).getCurrentLocation();
	// }
	// else {
	// ref2 = connection.getSourceAnchor().getReferencePoint();
	// }
	//
	// A_POINT.setLocation(connection.getSourceAnchor().getLocation(ref1));
	// connection.translateToRelative(A_POINT);
	// points.addPoint(A_POINT);
	//
	//		
	// A_POINT.setLocation(connection.getTargetAnchor().getLocation(ref2));
	// connection.translateToRelative(A_POINT);
	// // A_POINT.y = points.getPoint(0).y;
	// points.addPoint(A_POINT);
	//			
	// for(int i = 0; i < points.size(); i++) {
	// System.out.println(points.getPoint(i));
	// }
	// System.out.println();
	// connection.setPoints(points);
	// }
	//
	// public void setConstraint(Connection connection, Object constraint) {
	//			
	// }
	//		
	// }
	
}
